/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.org.underserver.servidor;
import org.wsl.controle.Controlador;
import br.org.underserver.server.VirtualServer;
import java.net.InetAddress;
import java.net.URL;
/**
 *
 * @author alexandre.araujo
 */
public class ServerHTTP{
    private Thread vst = null;
    private String host;
    private int port=8888;
    /**
     * Construtor do servidor de HTTP
     * 
     * @param host
     *            host do servidor
     * @param port
     *            porta do servidor
     */
    private static ServerHTTP instancia = null;
    static public ServerHTTP getInstancia(){
        if(instancia == null){
            instancia=new ServerHTTP();
        }
         return instancia;
    }
    private ServerHTTP() {
       // super();
    }     
        

    public String getIpHost(){  
          String ipLocalhost = "127.0.0.1";
          try{  
             ipLocalhost = InetAddress.getLocalHost().getHostAddress();  
          }catch(Exception e){  
                
          } 
          return ipLocalhost;                  
   }
    
    public static void addInfo(String msg){
        System.out.println(msg);
    }

    public static void iniciar() {
        if(ServerHTTP.getInstancia().vst==null){
            ServerHTTP.getInstancia().vst = new Thread(new VirtualServer(),"T2");
        }else{
            ServerHTTP.parar();
        }
        ServerHTTP.getInstancia().vst.start();
    }
    
    
    public static void parar(){
        ServerHTTP.addInfo("parando servidor...");
        if(ServerHTTP.getInstancia().vst!=null){            
            ServerHTTP.getInstancia().vst.interrupt();
            //Thread.currentThread().interrupt();
            ServerHTTP.addInfo("servidor parado");           
        }
    }	
    public static void setServicesConfig(URL urlconfig){
        ServerHTTP.getInstancia().setHost(ServerHTTP.getInstancia().getIpHost());
        Controlador.getInstancia().setConfig(urlconfig);               
    }
    
    public void setHost(String nhost){
        this.host = nhost;
    }
    
    public String getHost() {
        return host;
    }
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }
}
